package com.example.hellodemo.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.List;

public abstract class CommentAdapter<T> extends BaseAdapter {

    protected Context context;
    protected List<T> list;

    public CommentAdapter(Context context, List<T> list){
        this.context = context;
        this.list = list;
    }
    @Override
    public int getCount() {
        Log.d("getCount","" +list.size());
        return list.size();
    }


    @Override
    public T getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //得到一个ViewHolder
        CommonViewHolder viewHolder =  CommonViewHolder.get(context,convertView,parent,getViewLayoutId(position),position);
        //设置控件内容
        setViewContent(viewHolder, (T) getItem(position));
        return viewHolder.getConvertView();
    }

    /*
   * 获取cell对应的布局id  可能会有多样式的cell
    * */
    public  abstract int getViewLayoutId(int position);

    /**
     * 提供抽象方法，来设置控件内容
     *
     * @param viewHolder 一个ViewHolder
     * @param t          一个数据集
     */
    public  abstract void setViewContent(CommonViewHolder viewHolder, T t);

}
